/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pscombine.ftp;

import it.sauronsoftware.ftp4j.FTPAbortedException;
import it.sauronsoftware.ftp4j.FTPClient;
import it.sauronsoftware.ftp4j.FTPDataTransferException;
import it.sauronsoftware.ftp4j.FTPException;
import it.sauronsoftware.ftp4j.FTPFile;
import it.sauronsoftware.ftp4j.FTPIllegalReplyException;
import it.sauronsoftware.ftp4j.FTPListParseException;
import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import pscombine.constants.Constants;

/**
 *
 * @author srkrishnan
 */
public class FTP {

    static String lastimage="";
    
    public static FTPClient connectSrcFTP(String srcShop) {
        FTPClient srcftp = null;
        try {
            Configuration config = new PropertiesConfiguration("my.properties");
            srcftp = new FTPClient();
            srcftp.connect(config.getString(srcShop + ".hostname"));
            srcftp.login(config.getString(srcShop + ".username"), config.getString(srcShop + ".password"));
            srcftp.changeDirectory(config.getString(srcShop + ".basepath") + "img/p/");
            srcftp.setPassive(false);
            System.out.println("Connected to FTP... " + srcftp.getHost());
        } catch (IllegalStateException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (FTPIllegalReplyException ex) {
            ex.printStackTrace();
        } catch (FTPException ex) {
            ex.printStackTrace();
        } catch (ConfigurationException ex) {
            ex.printStackTrace();
        }
        return srcftp;
    }

    public static FTPClient connectDestFTP(String destShop) {
        FTPClient destftp = null;
        try {
            Configuration config = new PropertiesConfiguration("my.properties");
            destftp = new FTPClient();
            destftp.connect(config.getString(destShop + ".hostname"));
            destftp.login(config.getString(destShop + ".username"), config.getString(destShop + ".password"));
            destftp.changeDirectory(config.getString(destShop + ".basepath") + "img/p/");
            destftp.setPassive(false);
            System.out.println("Connected to FTP... " + destftp.getHost());
        } catch (IllegalStateException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (FTPIllegalReplyException ex) {
            ex.printStackTrace();
        } catch (FTPException ex) {
            ex.printStackTrace();
        } catch (ConfigurationException ex) {
            ex.printStackTrace();
        }
        return destftp;
    }

    public static void login() {
        try {
            Configuration config = new PropertiesConfiguration("my.properties");

            FTPClient client = new FTPClient();

            client.connect("cin400332l");
            client.login("anantha89", "secret");
            client.changeDirectory("/s1/img/p/");
            FTPFile[] list = client.list("1-*.jpg");

            for (FTPFile file : list) {
                System.out.println(file.getName());
            }

//            client.download("CodeIgniter_2.0.3.zip", new File("CodeIgniter_2.0.3.zip"));
//            
//            client.upload(new File("1-1.jpg"));

        } catch (FTPDataTransferException ex) {
            Logger.getLogger(FTP.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FTPAbortedException ex) {
            Logger.getLogger(FTP.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FTPListParseException ex) {
            Logger.getLogger(FTP.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalStateException ex) {
            Logger.getLogger(FTP.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(FTP.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FTPIllegalReplyException ex) {
            Logger.getLogger(FTP.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FTPException ex) {
            Logger.getLogger(FTP.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ConfigurationException ex) {
            ex.printStackTrace();
        }
    }

    public static void transferfile(String srcShop, String destShop, FTPClient srcclient, FTPClient destclient, int oldimageid, int productid, int newimageid, int newproductid) {
        Configuration config = null;
        try {
            config = new PropertiesConfiguration("my.properties");
        } catch (ConfigurationException ex) {
            ex.printStackTrace();
        }


        try {

 
            String imgidprefix = productid + "-" + oldimageid + ".*";

            while (srcclient.isConnected() == false) {
                srcclient = connectSrcFTP(srcShop);
            }
            srcclient.changeDirectory(config.getString(srcShop + ".basepath") + "img/p/");
            destclient.changeDirectory(config.getString(destShop + ".basepath") + "img/p/");
            FTPFile[] imglist = srcclient.list(imgidprefix);

            for (FTPFile file : imglist) {

                File downloadfile = new File(file.getName());
                while (srcclient.isConnected() == false) {
                    srcclient = connectSrcFTP(srcShop);
                }
                srcclient.download(file.getName(), downloadfile);

                String imgname = file.getName();
                imgname = imgname.replaceFirst(productid + "-" + oldimageid, newproductid + "-" + newimageid);
                while (destclient.isConnected() == false) {
                    destclient = connectDestFTP(destShop);
                }
                destclient.upload(new File(file.getName()));
                while (destclient.isConnected() == false) {
                    destclient = connectDestFTP(destShop);
                }
                destclient.rename(file.getName(), imgname);

                downloadfile.delete();

                System.out.println(file.getName() + " " + imgname);
                getTimeDiff(Constants.starttime, Calendar.getInstance().getTimeInMillis());
            }

//            if (lastimage == null ? "product_mini_" + productid + ".jpg" != null : !lastimage.equals("product_mini_" + productid + ".jpg")) {
//                while (srcclient.isConnected() == false) {
//                    srcclient = connectSrcFTP(srcShop);
//                }
//                srcclient.changeDirectory(config.getString(srcShop + ".basepath") + "img/tmp/");
//                String srcimg = "product_mini_" + productid + ".jpg";
//                System.out.println("Checking... "+srcimg);
//                if(srcclient.list(srcimg).length>0){
//                srcclient.download(srcimg, new File(srcimg));
//
//                while (destclient.isConnected() == false) {
//                    destclient = connectDestFTP(destShop);
//                }
//                destclient.changeDirectory(config.getString(destShop + ".basepath") + "img/tmp/");
//                destclient.upload(new File(srcimg));
//                while (destclient.isConnected() == false) {
//                    destclient = connectDestFTP(destShop);
//                }
//                destclient.changeDirectory(config.getString(destShop + ".basepath") + "img/tmp/");
//                destclient.rename(srcimg, "product_mini_" + newproductid + ".jpg");
//
//                new File(srcimg).delete();
//
//                System.out.println(new File(srcimg).getName() + " " + "product_mini_" + newproductid + ".jpg");
//                }
//                System.out.println(new File(srcimg).getName() + " " + "product_mini_" + newproductid + ".jpg not found!!");
//            }
            lastimage = "product_mini_" + productid + ".jpg";
            getTimeDiff(Constants.starttime, Calendar.getInstance().getTimeInMillis());
        } catch (FTPDataTransferException ex) {
            ex.printStackTrace();
        } catch (FTPAbortedException ex) {
            ex.printStackTrace();
        } catch (FTPListParseException ex) {
            ex.printStackTrace();
        } catch (IllegalStateException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (FTPIllegalReplyException ex) {
            ex.printStackTrace();
        } catch (FTPException ex) {
            ex.printStackTrace();
        } catch (Exception ex) {
            ex.printStackTrace();
        }


    }

    public static void getTimeDiff(long milliseconds1, long milliseconds2) {

        long diff = milliseconds2 - milliseconds1;
        long diffSeconds = diff / 1000;
        long diffMinutes = diff / (60 * 1000);
        long diffHours = diff / (60 * 60 * 1000);
        long diffDays = diff / (24 * 60 * 60 * 1000);
        System.out.println(diffDays + " Days " + diffHours + " Hours " + diffMinutes + " Minutes " + diffSeconds + " Secs");
    }
}
